package com.an.test.springjdbc;

/**
 * ClassName: SequenceNumber <br/>
 * Function: Thread简单示例. <br/>
 * Reason: Thread简单示例. <br/>
 * date: 2017年12月1日 下午3:22:51 <br/>
 *
 * @author atc
 * @version 1.0.0
 * @since JDK 1.8
 */
public class SequenceNumber {

	/**
	 * seqNum:TODO用一句话描述这个变量表示什么.
	 * 
	 * @since JDK 1.8
	 */
	private static ThreadLocal<Integer> seqNum = new ThreadLocal<Integer>() {
		// 指定初始值
		protected Integer initialValue() {
			return 0;
		}
	};

	// 获取 下一个序列值
	public int getNextNum() {
		seqNum.set(seqNum.get() + 1);
		return seqNum.get();
	}

	/**
	 * main:这里用一句话描述这个方法的作用. <br/>
	 * TODO这里描述这个方法适用条件 – 可选.<br/>
	 *
	 * @author atc
	 * @param args
	 * @since JDK 1.8
	 */
	public static void main(String[] args) {

		SequenceNumber sequenceNumber = new SequenceNumber();

		TestClient t1 = new TestClient(sequenceNumber);
		TestClient t2 = new TestClient(sequenceNumber);
		TestClient t3 = new TestClient(sequenceNumber);

		t1.start();
		t2.start();
		t3.start();

	}

	private static class TestClient extends Thread {
		private SequenceNumber sn;

		public TestClient(SequenceNumber sn) {
			this.sn = sn;
		}

		@Override
		public void run() {
			for (int i = 0; i < 3; i++) {
				// 每个线程打印三个序列值
				System.out.println("thread[" + Thread.currentThread().getName() + "] an[" + sn.getNextNum() + "]");
			}
		}

	}

}
